package com.wanbangee.marksystem.service.imp;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.wanbangee.marksystem.dao.StatisticsScoreTargetDao;
import com.wanbangee.marksystem.dao.StatisticsSubReportDao;
import com.wanbangee.marksystem.entities.StatisticsScoreTarget;
import com.wanbangee.marksystem.entities.StatisticsSubReport;
import com.wanbangee.marksystem.service.ScoreAndAnswerSheetService;

/**
 * 成绩与答案的业务实现类
 * @author yongcode
 *
 */
@Service
public class ScoreAndAnswerSheetServiceImpl implements ScoreAndAnswerSheetService {
	
	@Autowired
	private StatisticsScoreTargetDao statisticsScoreTargetDao;
	
	@Autowired
	private StatisticsSubReportDao statisticsSubReportDao;
	
	@Override
	public List<StatisticsScoreTarget> selectScoreSubsectionByExamIdAndSubId(Integer examId, Integer subId) {
		
		//根据考试Id和科目Id调用ScoreAndAnswerSheetDao中selectScoreSubsectionByExamIdAndSubId方法，查询到该科目全部的分段信息
		return statisticsScoreTargetDao.selectScoreSubsectionByExamIdAndSubId(examId, subId);
	}

	@Override
	@Transactional   //事务
	public Map<String, Object> insertStatisticsScoreTarget(JSONArray jsonArray) {
		
		//用来存储提示信息，和状态码，数据
		Map<String, Object> responseMap = new HashMap<String, Object>();
		
		
		//保存分数段信息
		//一：删除本次考试本科目的全部分数段信息
		if(jsonArray != null) {
			int result = statisticsScoreTargetDao.deleteScoreSubsectionByExamIdAndSubId(jsonArray.getJSONObject(0).getInteger("examId"), jsonArray.getJSONObject(0).getInteger("subId"));
			if(result == 0) {
				responseMap.put("code", 10013);
				responseMap.put("msg", "删除分数段表失败");
			}
		}
		
		//二：将新的重新添加进去
		
			//把接受字符串解析成json对象进行操作
			//JSONArray ja = JSONArray.parseArray(subsection);
			
	        for(int i=0;i<jsonArray.size();i++) {
	            // 遍历 jsonarray 数组，把每一个对象转成 json 对象
	        	JSONObject jb = jsonArray.getJSONObject(i);
	        	
	        	//创建分数段对象
	        	StatisticsScoreTarget sst = new StatisticsScoreTarget();
	        	
	        	sst.setTargetStartScore(jb.getDouble("startScore"));
	        	sst.setTargetEndScore(jb.getDouble("endScore"));
	        	sst.setExamId(jb.getInteger("examId"));
	        	sst.setSubId(jb.getInteger("subId"));
	        	sst.setSubNm(jb.getString("subNm"));
	        	sst.setAddTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));  //将当前时间转换字符串
	        	sst.setAddUserId(1);   //设置添加人的Id，即登录人的ID
	        	
	        	//将新建的分数段对象添加到数据库中
	        	int result = statisticsScoreTargetDao.insertStatisticsScoreTarget(sst);
	        	if(result == 0) {
					responseMap.put("code", 10013);
					responseMap.put("msg", "插入分数段表失败");
				}
	        }
	        
	        return responseMap;
	}

	@Override
	public List<StatisticsSubReport> selectStuScoreAndAnswerSheet(Integer examId, Integer claId, Integer subId,
			String stuNmOrStuNo,Integer pageNum,Integer pageSize) {
		PageHelper.startPage(pageNum,pageSize);
		return statisticsSubReportDao.selectStuScoreAndAnswerSheet(examId, claId, subId, "%"+ stuNmOrStuNo +"%");
	}

	@Override
	public Map<String, Object> insertStatisticsScoreTarget(Integer examId, Integer subId, String subNm,
			String subsection) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String selectStuExamaSheetUrl(Integer examId, Integer stuId, Integer subId) {
		// TODO Auto-generated method stub
		return null;
	}


}
